home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / libsipp / sipp_pix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-03  |  2.8 KB  |  129 lines

  1. /**
  2.  ** sipp - SImple Polygon Processor
  3.  **
  4.  **  A general 3d graphic package
  5.  **
  6.  **  Copyright Equivalent Software HB  1992
  7.  **
  8.  ** This program is free software; you can redistribute it and/or modify
  9.  ** it under the terms of the GNU General Public License as published by
  10.  ** the Free Software Foundation; either version 1, or any later version.
  11.  ** This program is distributed in the hope that it will be useful,
  12.  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  ** GNU General Public License for more details.
  15.  ** You can receive a copy of the GNU General Public License from the
  16.  ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  **/
  18.  
  19. /**
  20.  ** sipp_pixmap.c - A sample pixmap for use in SIPP and routines to use it.
  21.  **/
  22.  
  23. #include <sys/types.h>
  24. #include <stdio.h>
  25.  
  26. #include <sipp.h>
  27. #include <smalloc.h>
  28. #include <sipp_pixmap.h>
  29.  
  30.  
  31. /* ================================================================ */
  32.  
  33.  
  34. /*
  35.  * Return a pointer to a new Sipp_pixmap, with the size WIDTH x HEIGHT.
  36.  */
  37.  
  38. Sipp_pixmap *
  39. sipp_pixmap_create(width, height)
  40.     int   width;
  41.     int   height;
  42. {
  43.     Sipp_pixmap  * pm;
  44.  
  45.     pm = (Sipp_pixmap *) smalloc(sizeof(Sipp_pixmap));
  46.     pm->width = width;
  47.     pm->height = height;
  48.     pm->buffer = (u_char *) scalloc(3 * width * height, sizeof(u_char));
  49.  
  50.     return pm;
  51. }
  52.  
  53.  
  54.  
  55. /*
  56.  * Destruct a pixmap, and free allocated memory.
  57.  */
  58.  
  59. void
  60. sipp_pixmap_destruct(pm)
  61.     Sipp_pixmap  * pm;
  62. {
  63.     if (pm != NULL) {
  64.         if (pm->buffer != NULL) {
  65.             sfree(pm->buffer);
  66.         }
  67.         sfree(pm);
  68.     }
  69. }
  70.  
  71.  
  72.  
  73.  
  74. /*
  75.  * Set the pixel at (X, Y) in Sipp_pixmap PM to the color
  76.  * (RED, GRN, BLU).  
  77.  */
  78.  
  79. void
  80. sipp_pixmap_set_pixel(pm, x, y, red, grn, blu)
  81.     Sipp_pixmap  * pm;
  82.     int            x;
  83.     int            y;
  84.     u_char         red;
  85.     u_char         grn;
  86.     u_char         blu;
  87. {
  88.     u_char  * cp;
  89.  
  90.     if (x < 0 || y < 0 || x >= pm->width || y >= pm->height) {
  91.         fprintf(stderr, 
  92. "Tried to write to pixel (%d, %d) in a Sipp_pixmap which was only %d x %d.\n",
  93.                 x, y, pm->width, pm->height);
  94.     } else {
  95.         cp = pm->buffer + 3 * (y * pm->width + x);
  96.         *cp++ = red;
  97.         *cp++ = grn;
  98.         *cp = blu;
  99.     }
  100. }
  101.  
  102.  
  103.  
  104. /*
  105.  * Write the Sipp_pixmap PM to the open file FILE.
  106.  */
  107.  
  108. void
  109. sipp_pixmap_write(file, pm)
  110.     FILE         * file;
  111.     Sipp_pixmap  * pm;
  112. {
  113.     u_char  * byte;
  114.     int       nbytes;
  115.     int       i;
  116.  
  117.     fprintf(file,  "P6\n");
  118.     fprintf(file,  "#Image rendered with SIPP %s\n",  SIPP_VERSION);
  119.     fprintf(file,  "%d\n%d\n255\n", pm->width, pm->height);
  120.  
  121.     byte = pm->buffer;
  122.     nbytes = pm->width * pm->height;
  123.     for (i = 0; i < nbytes; ++i) {
  124.         putc(*byte++, file);
  125.         putc(*byte++, file);
  126.         putc(*byte++, file);
  127.     }
  128. }
  129.